package cn.danger.com.presenter;

import android.content.Context;

import cn.danger.com.app.MyApplication;
import cn.danger.com.model.action.LoginModel;
import cn.danger.com.model.action.LoginModelImpl;
import cn.danger.com.model.listener.OnCommonListener;
import cn.danger.com.util.Constants;
import cn.danger.com.util.FileCacheService;
import cn.danger.com.view.CommonView;

/**
 * Created by 俊 on 2016/12/28.
 */
public class LoginPresenterImpl implements LoginPresenter {

    private Context context;
    private CommonView view;
    private LoginModel model;

    public LoginPresenterImpl(Context context, CommonView view) {
        this.context = context;
        this.view = view;
        model = new LoginModelImpl(context);
    }

    @Override
    public void login(final String phone, final String password) {
        view.showDialog();
        model.login(phone, password, new OnCommonListener() {
            @Override
            public void onSuccess(String message) {
                new FileCacheService(context).saveUserInfo(phone, password);
                MyApplication.setOperationCode(Constants.NETWORK);
                model.synchronousData(phone, new OnCommonListener() {
                    @Override
                    public void onSuccess(String message) {
                        view.showMessage(Constants.SUCCESS_TO_GET_USERDATA_FROM_SERVER);
                    }

                    @Override
                    public void onError(String error) {
                        view.showMessage(error);
                    }
                });
                view.hideDialog();
                view.showMessage(Constants.SUCCESS_TO_LOGIN);
            }

            @Override
            public void onError(String error) {
                view.hideDialog();
                view.showMessage(error);
            }
        });
    }
}
